package com.java.test;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;

public class TableLayout implements LayoutManager, java.io.Serializable {

	private static final long serialVersionUID = 5154863521922195727L;

	int hgap;
	int vgap;
	int cols;
	public boolean debug = false;

	public TableLayout() {
		this(1, 0, 0);
	}

	public TableLayout(int cols) {
		this(cols, 0, 0);
	}

	public TableLayout(int cols, int hgap, int vgap) {
		setColumns(cols);
		this.hgap = hgap;
		this.vgap = vgap;
	}

	public void addLayoutComponent(String name, Component comp) {
	}

	public int getColumns() {
		return this.cols;
	}

	public int getHgap() {
		return this.hgap;
	}

	public int getVgap() {
		return this.vgap;
	}

	public void layoutContainer(Container parent) {
		synchronized (parent.getTreeLock()) {
			Insets insets = parent.getInsets();
			int ncomponents = parent.getComponentCount();
			int rows = ncomponents / this.cols;

			int[] w = new int[this.cols];
			int[] h = new int[rows];
			for (int col = 0; col < this.cols; col++)
				w[col] = 0;
			for (int row = 0, i = 0; row < rows; row++) {
				h[row] = 0;
				for (int col = 0; col < this.cols; col++) {
					Component comp = parent.getComponent(i++);
					Dimension d = comp.getPreferredSize();
					if (w[col] < d.width)
						w[col] = d.width;
					if (h[row] < d.height)
						h[row] = d.height;
				}
			}

			for (int row = 0, i = 0, y = insets.top; row < rows; row++) {
				int rh = h[row];
				for (int col = 0, x = insets.left; col < this.cols; col++) {
					Component comp = parent.getComponent(i++);
					int cw = w[col];
					if (col == (this.cols - 1)) {
						int ww = parent.getWidth() - x - insets.right;
						comp.setBounds(x, y, (ww > 0) ? ww - this.hgap : cw, rh);
					} else {
						comp.setBounds(x, y, cw, rh);
						x += this.hgap + cw;
					}
				}
				y += this.vgap + rh;
			}
		}
	}

	public Dimension minimumLayoutSize(Container parent) {
		return preferredLayoutSize(parent);
	}

	public Dimension preferredLayoutSize(Container parent) {
		synchronized (parent.getTreeLock()) {
			Insets insets = parent.getInsets();
			int ncomponents = parent.getComponentCount();
			int rows = ncomponents / this.cols;

			int[] w = new int[this.cols];
			for (int col = 0; col < this.cols; col++)
				w[col] = 0;
			int h = 0;
			for (int row = 0, i = 0; row < rows; row++) {
				int hh = 0;
				for (int col = 0; col < this.cols; col++) {
					if (i < ncomponents) {
						Component comp = parent.getComponent(i++);
						Dimension d = comp.getPreferredSize();
						if (w[col] < d.width)
							w[col] = d.width;
						if (hh < d.height)
							hh = d.height;
					}
				}
				h += hh;
			}

			int hh = insets.top + insets.bottom + (rows - 1) * this.vgap + h;
			int ww = insets.left + insets.right + (this.cols - 1) * this.hgap;
			for (int col = 0; col < this.cols; col++)
				ww += w[col];

			return new Dimension(ww, hh);
		}
	}

	public Dimension maximumLayoutSize(Container paramContainer) {
		return new Dimension(2147483647, 2147483647);
	}

	public void removeLayoutComponent(Component comp) {

	}

	public void setColumns(int cols) {
		if (cols < 1) {
			throw new IllegalArgumentException("'cols' must be > 0");
		}
		this.cols = cols;
	}

	public void setHgap(int hgap) {
		this.hgap = hgap;
	}

	public void setVgap(int vgap) {
		this.vgap = vgap;
	}

	public String toString() {
		return getClass().getName() + "[hgap=" + this.hgap + ",vgap="
				+ this.vgap + ",cols=" + this.cols + "]";
	}
}
